#ifndef EVENT_H
#define EVENT_H

#include "ByteString.h"

namespace ZY {

    enum EVENT {
        EVENT_CLOSE ,
        EVENT_INIT ,
        EVENT_DESTORY ,
        EVENT_ACCPET ,
        EVENT_WRITE_EMPTY ,
        EVENT_WRITE_NOBUFS ,
        EVENT_IN ,
        EVENT_OUT ,
        EVENT_MOVE ,
        EVENT_DONE
    };

    class Event
    {
        public :
            typedef void (*CallBack)( Event *ev );
            typedef void (*CallBackWithObject)( void *object , Event *ev );
            ByteString data;
            void *ptr;

            void assign( Event &ev );
    };

    class EventBase
    {
        private:
            struct CallBackOfObject {
                Event::CallBackWithObject fn;
                void *object;
            } ;
        public:
            EventBase();
            EventBase( const EventBase & eb );
            virtual ~EventBase();

            EventBase& copyFrom( const EventBase & eb );
            EventBase& operator=( const  EventBase & eb );

            //添加一个实例的监听器
            void addEventListener( int eventId , Event::CallBack fn );
            void addEventListener( int eventId , Event::CallBackWithObject fn , void *object );
            //移除一个实例上的所有对应事件的监听器
            void removeEventListener( int eventId );
            //移除一个指定事件的指定监听器
            void removeEventListener( int eventId , Event::CallBack fn );

            void removeEventListener( int eventId , Event::CallBackWithObject fn , void *object );
            void removeAllEvent();
            void removeAllObjectEvent();
            void removeAllBasicEvent();
            //
            bool callEventListener( int eventId , Event *ev );

            bool callAllEventListener( int eventId , Event *ev ) {bool ret=callEventListener( eventId , ev );return callStaticEventListener( eventId , ev ) || ret;}

            static void addStaticEventListener( int eventId , Event::CallBack fn );
            static void addStaticEventListener( int eventId , Event::CallBackWithObject fn , void *object );
            static void removeStaticEventListener( int eventId );
            static void removeStaticEventListener( int eventId , Event::CallBack fn );
            static void removeStaticEventListener( int eventId , Event::CallBackWithObject fn , void *object );
            static bool callStaticEventListener( int eventId , Event *ev );

        protected:
            map<int , vector<Event::CallBack> > m_event;
            map<int , vector<CallBackOfObject> > mo_event;
            static map<int, vector<Event::CallBack> > s_event;
            static map<int, vector<CallBackOfObject> > so_event;

    };
}
#endif // EVENT_H
